package cn.demos.controller;

import cn.demos.pojo.Faqs;
import cn.demos.service.impl.FaqsServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class FaqsController {

    @Resource
    private FaqsServiceImpl faqsService;
    @RequestMapping("/all.do")
    public String getAll(String title, HttpSession session){
        List<Faqs> list = faqsService.getAll(title);
        if (list==null||list.isEmpty()){
            session.setAttribute("msg","没有找到与“"+title+"”相关的内容，");
            session.setAttribute("msg2","请修改关键字后重试。");
        }
        else{
            session.setAttribute("msg",null);
        }
        session.setAttribute("list",list);
        return "index.jsp";
    }

    @RequestMapping("/info.do")
    public String info(Integer id,HttpSession session){
        Faqs faqs = faqsService.getById(id);
        session.setAttribute("faqs",faqs);
        return "info.jsp";
    }

    @RequestMapping("/check.do")
    @ResponseBody
    public Map check(String title){
        Map map = new HashMap();
        int count = faqsService.check(title);
        System.out.println(count);
        if (count>0){
            map.put("result",false);
        }else {
            map.put("result",true);
        }
        return map;
    }

    @RequestMapping("/add.do")
    @ResponseBody
    public Map add(Faqs faqs){
        Map map = new HashMap();
        int count = faqsService.add(faqs);
        System.out.println(count);
        if (count>0){
            map.put("result",true);
        }else {
            map.put("result",false);
        }
        return map;
    }
}
